<?php 
/*************************************************************************************
 * doeditpoint_inc.php
 * -----------
 * phpGMapTrips 
 * 
 * This php application allows you to easily record your travel destinations 
 * and display them with Google maps.
 * 
 * Author: Stephane Coste (lescoste@gmail.com)
 * Copyright: (c) 2006 Stephane Coste (http://lescoste.net/)
 * Release Version: 0.1
 * CVS Revision Version: $Revision: 1.10 $
 * Date Started: 2006/02/19
 * Last Modified: $Date: 2007/03/14 14:32:43 $
 *************************************************************************************
 *
 *     This file is part of phpGMapTrips.
 *
 *   phpGMapTrips is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   phpGMapTrips is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with phpGMapTrips; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ************************************************************************************/
?>
<?PHP

include_once("utils.php");
include_once("dbaccess.php");

$index = 0;
$indexnew = 0;
$noproblemo = true;
$hasBeenSaved = false;

$editNewname = replace_accents($editNewname);
$editTags = replace_accents($editTags);

$tmpfilename = $f_files.rand().".tmp";
$fpw = fopen($tmpfilename, "w");

if (flock($fpw, LOCK_EX)) { // do an exclusive lock
	fwrite($fpw, '<?xml version="1.0"?>');
	fwrite($fpw, '<points>');
	fwrite($fpw, "\r\n");

	if (empty($editName) && empty($editNewname)) {
		echo $lang_error_tryagain;
		$noproblemo = false;
	} else {

		$hasBeenSaved = savePoint($fpw, $hasBeenSaved, $editName, $editNewname, $editLat, $editLng, $editUrl, $editPhotoUrl, $editTags, $editMoreinfo);

		if (!$hasBeenSaved) {
			// add point
			$editNewname = getUniquePointName($editNewname);
			echo '<br/>'.$lang_place.' '.$editNewname.' '.$lang_place_added.'.';
			fwrite($fpw, printPoint($editNewname, $editLat, $editLng, $editUrl, $editPhotoUrl, $editTags, $editMoreinfo));
			$hasBeenSaved = true;
		}
	}

	fwrite($fpw, '</points>');
	flock($fpw, LOCK_UN); // release the lock
	fclose($fpw);  
	copy($f_files, $f_files.'.old');
	copy($tmpfilename, $f_files);
	unlink($tmpfilename);
} else {
	echo "Couldn't lock the file !";
	fclose($fpw);  
}

function printPoint($destiName, $destiLat, $destiLng, $destiUrl, $destiPhotoUrl, $trips, $destiArray) {
	global $index, $moreinfoNames;
	$r = "";
	$r .= "<point name=\"".$destiName."\" lat=\"".$destiLat."\" long=\"".$destiLng."\"  url=\"".$destiUrl."\" trips=\"".$trips."\">";

         foreach($moreinfoNames as $name) {
		$moreinfoValue = $destiArray[$name];
                if (!empty($moreinfoValue)) {
			$moreinfoValue = replace_accents($moreinfoValue);
			$r .= "<moreinfo name=\"".$name."\">".$moreinfoValue."</moreinfo>";
                }
         }

	$r .= "<photos><photo url=\"".$destiPhotoUrl."\" /></photos>";
	$r .= "</point>\n";
	$index++;
	return $r;
}
?>

